package com.blog.service.impl;

import com.blog.mapper.ArticleTagMapper;
import com.blog.pojo.ArticleTag;
import com.blog.pojo.Tag;
import com.blog.service.ArticleTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleTagServiceImpl implements ArticleTagService {

    @Autowired
    private ArticleTagMapper articleTagMapper;

    // 查看文章的所有标签
    @Override
    public List<Tag> listByArticleId(Integer id) {
        List<Tag> tagList = articleTagMapper.listByArticleId(id);
        return tagList;
    }

    // 删除文章标签
    @Override
    public void deleteByArticleId(Integer articleId) {
        articleTagMapper.deleteByArticleId(articleId);
    }

    // 添加文章标签
    @Override
    public void add(Integer articleId, List<Integer> tagIds) {
        for(Integer tagId : tagIds) {
            ArticleTag articleTag = new ArticleTag();
            articleTag.setArticleId(articleId);
            articleTag.setTagId(tagId);
            articleTagMapper.insert(articleTag);
        }
    }

}
